import random
import time as tt
from urllib.parse import urlparse

from application.db.mysql_db.nsfc.NsfcInfoAttachmentList import NsfcInfoAttachmentList
from application.db.mysql_db.nsfc.NsfcInfoList import NsfcInfoList
from application.db.mysql_db.nsfc.NsfcInfoSectionList import NsfcInfoSectionList
from application.etl.write.base_batch_observer import BaseBatchObserver
from application.db import get_database_connection
from application.db.mysql_db.info.ResourceInformationAttachmentList import ResourceInformationAttachmentList
from application.db.mysql_db.info.ResourceInformationList import ResourceInformationList
from application.db.mysql_db.info.ResourceInformationSectionList import ResourceInformationSectionList
from application.db.mysql_db.info.ResourceInformationTagsRelation import ResourceInformationTagsRelation
from application.db.mysql_db.info.ResourceSourceDict import ResourceSourceDict


class NsfcToNsfcWrite(BaseBatchObserver):
    """
    资源信息批量观察者类，负责处理资源信息数据并写入数据库
    """

    @get_database_connection("default1").atomic()  # 保证事务
    def process_batch(self, batch_data):
        """
        批量处理并写入数据库
        """
        print(f"\n[ResourceBatchObserver] 批量处理 {len(batch_data)} 行数据:")
        print("=" * 50)

        information_list = []
        information_section = []
        information_attachment = []
        for value_dict in batch_data:
            info_name = value_dict.get('information_name')._value['zh']
            information_list.append({
                'information_id': value_dict.get('information_id'),
                'info_type_id': value_dict.get('info_type_id'),
                'apply_code': value_dict.get('apply_code'),
                'source_id': value_dict.get('source_id'),
                'info_name': info_name,
                'original_link': value_dict.get('original_link'),
                'publish_date': value_dict.get('publish_date'),
            })
            if value_dict['info_section']:
                information_section.extend(value_dict['info_section'])
            if value_dict['info_attachment']:
                information_attachment.extend(value_dict['info_attachment'])
        for idx, sec in enumerate(information_section):
            sec = self.convert_to_dict(sec)
            sec['is_del'] = 0
            information_section[idx] = sec  # 写回列表
        for idx, sec in enumerate(information_attachment):
            sec = self.convert_to_dict(sec)
            sec['is_del'] = 0
            information_attachment[idx] = sec  # 写回列表
        NsfcInfoList.insert_many(information_list).execute()
        NsfcInfoSectionList.insert_many(information_section).execute()
        NsfcInfoAttachmentList.insert_many(information_attachment).execute()

    def convert_to_dict(self, obj):
        """
        将 Pathway 的 Json 对象转换为 Python 字典
        """
        return obj.as_dict()
